package com.sun.alen.controller;


import com.sun.alen.entity.*;
import com.sun.alen.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/course")
@Slf4j
public class CourseController {
    @Autowired
    private CourseService courseService;


    /**
     * 获取所有老师
     * @return
     */
    @GetMapping("/all")
    public Result all(){
        List<Course> courseList=courseService.all();
        return Result.success(courseList);
    }

    /**
     * 添加课程
     * @param course
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Course course){
        boolean flag=courseService.add(course);
        if (flag){
            return Result.success();
        }else {
            return Result.error("添加失败");
        }
    }

    /**
     * 修改课程
     * @param course
     * @return
     */
    @PutMapping
    public Result update(@RequestBody Course course){
        boolean flag=courseService.update(course);
        if (flag){
            return Result.success();
        }else {
            return Result.error("修改失败");
        }
    }

    /**
     * 根据ID删除课程
     * @param id
     * @return
     */
    @DeleteMapping
    public Result delete(String id){
        boolean flag=courseService.delete(id);
        if (flag){
            return Result.success();
        }else {
            return Result.error("删除失败");
        }
    }

    /**
     * 搜索
     * @param input
     * @param select
     * @param currentPage
     * @param pageSize
     * @return
     */
    @GetMapping("/search")
    public Result search(@RequestParam(name = "input",required = false,defaultValue = "")String input,
                         @RequestParam(name = "select",required = false,defaultValue = "1")String select,
                         @RequestParam(name = "page",required = false,defaultValue = "1")Integer currentPage,
                         @RequestParam(name = "size",required = false,defaultValue = "5")Integer pageSize){
        PageBean pageBean=courseService.search(input,select,currentPage,pageSize);
        return Result.success(pageBean);
    }

    /**
     * 搜索
     * @param input
     * @param select
     * @param currentPage
     * @param pageSize
     * @return
     */
    @GetMapping("/searchTeacher")
    public Result searchByTeacher(@RequestParam(name = "input",required = false,defaultValue = "")String input,
                         @RequestParam(name = "select",required = false,defaultValue = "1")String select,
                         @RequestParam(name = "teacherId",required = false)Integer teacherId,
                         @RequestParam(name = "page",required = false,defaultValue = "1")Integer currentPage,
                         @RequestParam(name = "size",required = false,defaultValue = "5")Integer pageSize){

        PageBean pageBean=courseService.searchTeacher(input,select,teacherId,currentPage,pageSize);
        return Result.success(pageBean);
    }

}
